package conf

import (
	"fmt"
	"gopkg.in/yaml.v2"
	"os"
	"path/filepath"
	"strings"
)

type Config struct {
	Service struct {
		AppMode  string `yaml:"AppMode"`
		Address  string `yaml:"Address"`
		HttpPort string `yaml:"HttpPort"`
	} `yaml:"service"`
	Mysql struct {
		Host     string `yaml:"Host"`
		Port     string `yaml:"Port"`
		Username string `yaml:"Username"`
		Password string `yaml:"Password"`
		Database string `yaml:"Database"`
	} `yaml:"mysql"`
	Redis struct {
		Host     string `yaml:"Host"`
		Port     string `yaml:"Port"`
		Password string `yaml:"Password"`
	} `yaml:"redis"`
	Minio struct {
		AccessKey string `yaml:"AccessKey"`
		SecretKey string `yaml:"SecretKey"`
		Bucket    string `yaml:"Bucket"`
		Server    bool   `yaml:"Server"`
	} `yaml:"minio"`
}

func getConf() Config {
	filePath := filepath.Join(ReturnConfig().confPath, "config.yaml")
	file, err := os.ReadFile(filePath)
	if err != nil {
		fmt.Println(err)
	}
	var config Config
	err = yaml.Unmarshal(file, &config)
	if err != nil {
		fmt.Println(err)
	}
	return config
}

func HttpPort() string {
	http_port := getConf().Service
	return strings.Join([]string{http_port.Address, ":", http_port.HttpPort}, "")
}

func MysqlConnect() string {
	var host string = getConf().Mysql.Host
	var port string = getConf().Mysql.Port
	var username string = getConf().Mysql.Username
	var password string = getConf().Mysql.Password
	var dbname string = getConf().Mysql.Database
	return strings.Join([]string{
		username, ":",
		password, "@tcp(", host, ":", port, ")/",
		dbname, "?charset=utf8mb4&parseTime=True&loc=Local"}, "")
}
